<?php
namespace Admin\Controller;

use Think\Controller;
use Admin\Model\DoctorsModel;
use Common\Model\HuanxinLogic;

class DoctorsController extends Controller {

    protected $instance = NULL;

    public function __construct() {
        parent::__construct();
        if ($this->instance == NULL) {
            $this->instance = new DoctorsModel;
        }
    }

    public function index() {
        $count = $this->instance->count();
        $Page = new \Think\Page($count, 10);
        $Page->setConfig('theme', "<ul class='pagination'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><span> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页 </span></li></ul>");
        $Pageshow = $Page->show();
        $index = [
            "limit" => array(
                "firstRow" => $Page->firstRow,
                "listRows" => $Page->listRows,
            ),
        ];
        $doctors = $this->instance->index($index);
        $this->assign('doctors', $doctors);
        $this->assign('Pageshow', $Pageshow);
        $this->display();
    }

    public function addDoctorsPage() {
        $HospitalObj = M('Hospital');
        $hospital = $HospitalObj->where('status = 1')->select();
        $this->assign('Hospital', $hospital);
        return $this->display();
    }

    public function ajaxKeshi() {
        $hospital_id = I('get.hospital_id');
        $keshiObj = M("hospital_keshi");
        $keshi = $keshiObj->join('left join keshi on hospital_keshi.keshi_id = keshi.id')->where('hospital_keshi.hospital_id = ' . $hospital_id)->field('hospital_keshi.keshi_id, keshi.name')->select();
        return $this->ajaxReturn($keshi);
    }

    public function addDoctors() {
        $data['hospital_id'] = I('post.hospital_id');
        $data['keshi_id'] = I('post.keshi_id');
        $data['phone'] = I('post.phone');
        $data['password'] = MD5(C('PASSWORD_SALT') . I('post.password'));
        $data['createtime'] = time();

        $userObj = M('user_hospital');
        if ($userObj->create($data, 1)) {
            $status = $userObj->add();

            //添加环信用户
            //添加环信用户
            $hx = new HuanxinLogic;
            $hx->addHospitalUser($status);

            if ($status) {
                return $this->success('添加成功', U('Admin/Doctors/index'));
            } else {
                return $this->success('添加失败', U('Admin/Doctors/addDoctorsPage'));
            }
        } else {
            return $this->success('添加失败', U('Admin/Doctors/addDoctorsPage'));
        }
    }

    public function editDoctorsPage() {
        $id = I('get.id');
        $DoctorsObj = M('user_hospital');
        $doctors = $DoctorsObj->find($id);

        $HospitalObj = M('hospital');
        $hospital = $HospitalObj->where('status = 1')->select();

        $KeshiObj = M('hospital_keshi');
        $keshi = $KeshiObj->join('left join keshi on hospital_keshi.keshi_id = keshi.id')->where("hospital_keshi.hospital_id=" . $doctors['hospital_id'])->select();

        $this->assign('Doctors', $doctors);
        $this->assign('Hospital', $hospital);
        $this->assign('Keshi', $keshi);
        return $this->display();

    }

    public function editDoctors() {
        $data['id'] = I('post.id');
        $data['hospital_id'] = I('post.hospital_id');
        $data['keshi_id'] = I('post.keshi_id');
        $data['phone'] = I('post.phone');

        $userObj = M('user_hospital');
        if ($userObj->create($data, 2)) {
            $status = $userObj->save();
            if ($status) {
                return $this->success('修改成功', U('Admin/Doctors/index'));
            } else {
                return $this->success('修改失败', U('Admin/Doctors/editDoctorsPage', ['id' => $data['id']]));
            }
        } else {
            return $this->success('修改失败', U('Admin/Doctors/editDoctorsPage', ['id' => $data['id']]));
        }

    }

}